define(function(require, exports){
	var $       = require("jquery");
	var util    = require('util');
	var pubjs   = require('pubjs');
	var view    = require('@base/view');
	var utilDSP = require('modules/common');

	var Main = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'uk-form',
				'urlSave': 'blacklist/add',
				'info': LANG('新建黑名单'),
				'vModel': {
					_id: '',
					domain: '',
					spot: '',
					appid: '',
					status: 1,
				},
				'vMethods': {
				}
			});

			this.$data = null;

			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			$(['<div class="P-adsAddContainer">',
					'<div class="M-formCon">',
						'<div class="M-formConGroup P-adsAddChannelCon">',
							'<label class="M-formLabel group-label">'+LANG('名称：')+'</label>',
							'<div class="group-content">',
								'<span pub-mod="@base/common/input.text" pub-name="blacklistName"/>',
							'</div>',
						'</div>',
						'<div class="M-formConGroup">',
							'<label class="group-label">'+LANG('广告主选择：')+'</label>',
							'<div class="group-content">',
								'<span pub-name="selectAdver" pub-mod="modules/selectAdver.base"></span>',
							'</div>',
						'</div>',
						'<div class="M-formConGroup P-adsAddChannelCon">',
							'<label class="M-formLabel group-label">'+LANG('黑名单内容：')+'</label>',
							'<div class="group-content">',
								'<div class="M-formTab" pub-name="tab" pub-mod="@base/common/base.tab"/>',
							'</div>',
						'</div>',
						'<div class="M-formConGroup P-adsAddChannelCon">',
							'<label class="M-formLabel group-label">'+LANG('状态：')+'</label>',
							'<div class="group-content">',
								'<div class="M-formSectionItem">',
									'<label class="mr10">',
										'<input type="radio" name="status" v-model="status" value="1">',
										'<span class="pl5">'+LANG('开启')+'</span>',
									'</label>',
									'<label>',
										'<input type="radio" name="status" v-model="status" value="2">',
										'<span class="pd5">'+LANG('关闭')+'</span>',
									'</label>',
								'</div>',
							'</div>',
						'</div>',
					'</div>',
					'<div class="M-formButtons">',
						'<span pub-name="cancel" pub-mod="@base/common/input.button" class="mr10" />',
						'<span pub-name="save" pub-mod="@base/common/input.button" />',
					'</div>',
				'</div>',
			].join('')).appendTo(el);

			var config = {
				tab: {
					'class': 'cool',
					'reqMethod': 'websocket',
					'silent': false,
					'list': {
						'domain':{
							'text': LANG('域名'),
							'module': 'admin/blacklist/tab/material.domain'
						},
						'appid':{
							'text': LANG('APPID'),
							'module': 'admin/blacklist/tab/material.appid'
						},
						'spot':{
							'text': LANG('媒体广告位'),
							'module': 'admin/blacklist/tab/material.mediaSpot'
						},
					}
				},
				blacklistName: {
					width: '50%',
					value: LANG('新建黑名单_')+ new Date().getTime()
				},
				cancel: {
					value: LANG('取消'),
					class: 'btn btn-link mr10 mt10'
				},
				save: {
					value: LANG('保存'),
					class: 'btn btn-confirm mt10'
				}
			};

			self.buildFromTemplate(config);

			self.vm.mount();
		},
		afterBuildTemplate: function(){

		},
		load: function(id, isCopy) {
			var self = this;

			self.showLoading();
			pubjs.mc.send('blacklist/info', {_id: +id}, self.onData.bind(self), isCopy);
			return self;
		},
		onData: function(err, data, isCopy) {
			this.hideLoading();
			if(err) {
				return pubjs.alert(err.message);
			}

			// 复制需要删除ID
			if (isCopy){
				data._id = null;
			}

			this.setData(data);
		},
		setData: function(data) {
			data = util.clone(data);
			var self = this;
			var vm = self.vm;
			var cs = self.$;

			self.$data = data;

			cs.blacklistName.setValue(data.Name);
			cs.selectAdver.setData(data.AdvertiserId);

			vm.set({'status': data.Status});

			cs.tab.setData(data.Domains, 'domain');
			cs.tab.setData(data.AppIds, 'appid');
			cs.tab.setData(data.Spots, 'spot');

			return this;
		},
		getData: function(){
			var data = {};
			var vm = this.vm.$;
			var cs = this.$;

			data.Name = util.trim(cs.blacklistName.getValue());
			data.AdvertiserId = cs.selectAdver.getData(true);


			var tabData = this.$.tab.getData();

			data.Domains = [];
			util.each(tabData.domain, function(item){
				data.Domains.push(item.Content);
			});

			data.AppIds = [];
			util.each(tabData.appid, function(item){
				data.AppIds.push(item.Content);
			});

			data.Spots = [];
			util.each(tabData.spot, function(item){
				data.Spots.push(+item);
			});


			data.Status = +vm.status;

			return data;
		},
		onInputClick: function(ev){
			switch (ev.name){
				case 'save':
					return this.save();
				case 'cancel':
					return this.cancel();
			}
		},
		cancel: function(){
			pubjs.controller.navigate('admin/blacklist');
		},
		validate: function(data){
			data = data || this.getData();

			if(!data.Name){
				pubjs.alert("请填写名称");
				return false;
			}

			if(!data.Domains.length && !data.AppIds.length && !data.Spots.length){
				pubjs.alert("请添加黑名单内容");
				return false;
			}

			return true;
		},
		save: function(){
			var self = this;
			var cs = self.$;
			var vm = self.vm.$;
			var data = self.getData();


			if(this.validate()){
				self.showLoading();
				if(self.$data && self.$data._id){
					data._id = self.$data._id;
					pubjs.mc.send('blacklist/update', data, self.onSave.bind(self));
				}else{
					pubjs.mc.send('blacklist/add', data, self.onSave.bind(self));
				}
			}
		},
		onSave: function(err, data){
			this.hideLoading();
			if (err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.controller.navigate('admin/blacklist');
		},
		reset: function(){
			var self = this;
			var cs = self.$;
			self.vm.reset();
			cs.blacklistName.setValue(LANG('新建黑名单_')+ new Date().getTime());
			cs.selectAdver.reset();
			cs.tab.reset();
			self.$data = null;
			cs.tab.switchTab('domain');
			return self;
		}
	});
	exports.main = Main;
});